package clientFrame;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class ClientPrive extends JFrame {
	
	private String nom;
	private ClientInterface clientInterface;
	private Boolean bloque;

	private JTextArea converse = new JTextArea();
	private JTextField message = new JTextField();
	private JButton envoyer;
	private JButton bloquer;
	private JPanel conversation;
	
	public ClientPrive(String n, ClientInterface ci)
	{
		this.nom=n;
		this.clientInterface = ci;
		this.bloque = false;
		
		try {
			// JFrame properties
			this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			this.setResizable(true);
			this.setTitle("Tchat - "+nom);
			this.setSize(600, 500);
			this.setDefaultCloseOperation(HIDE_ON_CLOSE);

			this.setLayout(null);


			converse.setPreferredSize(new Dimension(420, 220));
			converse.setLineWrap(true);
			message.setPreferredSize(new Dimension(230, 30));
			envoyer = new JButton("Envoyer");
			envoyer.setPreferredSize(new Dimension(80, 30));
			bloquer = new JButton();
			bloquer.setPreferredSize(new Dimension(180, 30));
			
			conversation = new JPanel(new FlowLayout());
			conversation.setBounds(10, 40, 580, 460);

			conversation.add(converse);
			conversation.add(message);
			conversation.add(envoyer);
			conversation.add(bloquer);

			this.add(conversation);

			addWindowListener(new WindowAdapter() {
				public void windowClosing(WindowEvent e) {
					
				}
			});

			envoyer.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					send();
				}
			});
			
			bloquer.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					bloquerDebloquer();
				}
			});


		} catch (Exception e) {
			System.err
					.println("<ServerInterface> - Error while creating interface : "
							+ e);
			e.printStackTrace();
		}
	}
	
	public void send() {
		try {
			clientInterface.getClient().setAction("SEND");
			clientInterface.getClient().sendToServer("SEND---" + nom.trim() + "---string---" + message.getText());
			if (!message.getText().equals(""))
				converse.setText(converse.getText()+nom+" dit :\n"+message.getText()+"\n");
			message.setText("");
		}

		catch (Exception ex) {
			System.out.println(ex.toString());
			/*
			 * liste.add(ex.toString());
			 * liste.makeVisible(liste.getItemCount()-1);
			 * liste.setBackground(Color.yellow);
			 */
		}
	}
	
	public void bloquerDebloquer() {
		try {
				if (bloque)
				{
					bloquer.setText("Bloquer ce contact");
					clientInterface.getClient().sendToServer("UNBLOCK---" + nom);
					bloque = false;
				}
				else
				{
					bloquer.setText("Debloquer ce contact");
					clientInterface.getClient().sendToServer("BLOCK---" + nom);
					bloque = true;
				}
		}

		catch (Exception ex) {
			System.out.println(ex.toString());
			/*
			 * liste.add(ex.toString());
			 * liste.makeVisible(liste.getItemCount()-1);
			 * liste.setBackground(Color.yellow);
			 */
		}
	}

	public void quit() {
		//this.setVisible(false);
	}

	
	public String getNom()
	{
		return nom;
	}
	public void setNom(String n)
	{
		this.nom=n;
	}
	public ClientInterface getClientInterface()
	{
		return clientInterface;
	}
	public void setClientInterface(ClientInterface ci)
	{
		this.clientInterface=ci;
	}
	public JTextArea getConverse()
	{
		return converse;
	}
	public void setBloque(Boolean b) {
		this.bloque = b;
	}
	public Boolean getBloque() {
		return bloque;
	}
	public void setTextButtonBloquer(String s)
	{
		bloquer.setText(s);
	}
}
